/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lczyfz.wisdom.modules.platform.web;

import com.lczyfz.wisdom.common.config.Global;
import com.lczyfz.wisdom.common.persistence.Page;
import com.lczyfz.wisdom.common.utils.StringUtils;
import com.lczyfz.wisdom.common.web.BaseController;
import com.lczyfz.wisdom.modules.platform.entity.Opinion;
import com.lczyfz.wisdom.modules.platform.service.OpinionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户反馈Controller
 * @author css
 * @version 2019-09-26
 */
@Controller
@RequestMapping(value = "${adminPath}/opinion/opinion")
public class OpinionController extends BaseController {

	@Autowired
	private OpinionService opinionService;
	
	@ModelAttribute
	public Opinion get(@RequestParam(required=false) String id) {
		Opinion entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = opinionService.get(id);
		}
		if (entity == null){
			entity = new Opinion();
		}
		return entity;
	}
	
	@RequiresPermissions("opinion:opinion:view")
	@RequestMapping(value = {"list", ""})
	public String list(Opinion opinion, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Opinion> page = opinionService.findPage(new Page<Opinion>(request, response), opinion);
		model.addAttribute("page", page);
		return "modules/opinion/opinionList";
	}

	@RequiresPermissions("opinion:opinion:view")
	@RequestMapping(value = "form")
	public String form(Opinion opinion, Model model) {
		model.addAttribute("opinion", opinion);
		return "modules/opinion/opinionForm";
	}

	@RequiresPermissions("opinion:opinion:edit")
	@RequestMapping(value = "save")
	public String save(Opinion opinion, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, opinion)){
			return form(opinion, model);
		}
		opinionService.save(opinion);
		addMessage(redirectAttributes, "保存opinion成功");
		return "redirect:"+ Global.getAdminPath()+"/opinion/opinion/?repage";
	}
	
	@RequiresPermissions("opinion:opinion:edit")
	@RequestMapping(value = "delete")
	public String delete(Opinion opinion, RedirectAttributes redirectAttributes) {
		opinionService.delete(opinion);
		addMessage(redirectAttributes, "删除opinion成功");
		return "redirect:"+Global.getAdminPath()+"/opinion/opinion/?repage";
	}

}